#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify, g

from ...controller.studentController import StudentController
from ...utils import commons
from ...utils.response_code import RET
from ...utils.validate_operate import verify_operator_object,verify_user_role


class StudentResource(Resource):

    # get
    @classmethod
    # @verify_user_role([2, 3])
    def get(cls, studentID=None):
        if studentID:   # 根据id查看信息时，增加对token的验证，验证是否是在查看自己的信息，防止越权查看他人信息
            print(g.user['user_id'])
            student_data = StudentController.get(**{'UserID': g.user['user_id']})
            student_id = student_data.get('data')[0]['studentID']    # 由user_id查询student_id ，对比请求的studentID
            if student_id != studentID:
                return jsonify(message='您无权查看他人信息')
            else:
                kwargs = {
                    'studentID': studentID
                }
                res = StudentController.get(**kwargs)
                if res['code'] == RET.OK:
                    return jsonify(code=res['code'], message=res['message'], data=res['data'])
                else:
                    return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('classID', location='args', required=False, help='classID参数类型不正确或缺失')
        parser.add_argument('collegeID', location='args', required=False, help='collegeID参数类型不正确或缺失')
        parser.add_argument('name', location='args', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('age', location='args', required=False, help='age参数类型不正确或缺失')
        parser.add_argument('agenda', location='args', required=False, help='agenda参数类型不正确或缺失')
        parser.add_argument('addTime', location='args', required=False, help='addTime参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = StudentController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    # delete

    @classmethod
    # @verify_user_role([2, 3])
    def delete(cls, studentID=None):
        if studentID:
            kwargs = {
                'studentID': studentID
            }

        else:
            parser = reqparse.RequestParser()
            parser.add_argument('classID', location='form', required=False, help='classID参数类型不正确或缺失')
            parser.add_argument('collegeID', location='form', required=False, help='collegeID参数类型不正确或缺失')
            parser.add_argument('name', location='form', required=False, help='name参数类型不正确或缺失')
            parser.add_argument('age', location='form', required=False, help='age参数类型不正确或缺失')
            parser.add_argument('agenda', location='form', required=False, help='agenda参数类型不正确或缺失')
            parser.add_argument('addTime', location='form', required=False, help='addTime参数类型不正确或缺失')
            
            # Pass in the ID list for multiple deletions
            parser.add_argument('studentID', type=str, location='form', required=False, help='studentID参数类型不正确或缺失')

            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

        res = StudentController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    @verify_operator_object([2,3],StudentController)
    def put(cls, studentID):
        if not studentID:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('classID', location='form', required=False, help='classID参数类型不正确或缺失')
        parser.add_argument('collegeID', location='form', required=False, help='collegeID参数类型不正确或缺失')
        parser.add_argument('name', location='form', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('age', location='form', required=False, help='age参数类型不正确或缺失')
        parser.add_argument('agenda', location='form', required=False, help='agenda参数类型不正确或缺失')
        parser.add_argument('addTime', location='form', required=False, help='addTime参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['studentID'] = studentID

        res = StudentController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # add
    @classmethod
    @verify_user_role([2, 3])
    def post(cls):
        '''
        StudentList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('StudentList', type=str, location='form', required=False, help='StudentList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('StudentList'):
            res = StudentController.add_list(**kwargs)

        else:
            parser.add_argument('studentID', location='form', required=False, help='studentID参数类型不正确或缺失')
            parser.add_argument('classID', location='form', required=False, help='classID参数类型不正确或缺失')
            parser.add_argument('collegeID', location='form', required=False, help='collegeID参数类型不正确或缺失')
            parser.add_argument('name', location='form', required=False, help='name参数类型不正确或缺失')
            parser.add_argument('age', location='form', required=False, help='age参数类型不正确或缺失')
            parser.add_argument('agenda', location='form', required=False, help='agenda参数类型不正确或缺失')
            parser.add_argument('addTime', location='form', required=False, help='addTime参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(field=[],**kwargs)

            res = StudentController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
